// pages/orders/orders/index.js
const app = getApp()
Page({

    data: {
        navItem: [
            {
                title: '全部',
                checked: false,
                name: 'all',
                url: "/order/all",
            },
            {
                title: '待付款',
                checked: false,
                name: 'arrearage',
                url: "/order/obligation",
            },
            {
                title: '已付款',
                checked: false,
                name: 'paid',
                url: "/order/paid",
            }
        ],
        url: app.globalData.url,
        list: undefined,
    },
    onLoad(options) {
        const user = wx.getStorageSync("user")
        if (user) {
            this.setData({ user })
        }
        // 按照点进来的菜单不同设置菜单字体颜色
        let navItem = this.data.navItem
        if (options) {
            if (options.item) {
                navItem.forEach(item => {
                    if (item.name === options.item) {
                        item.checked = true
                    }
                })
                this.initData(navItem)
            }
        }
        
    },
    // 初始化数据
    initData(navItem) {
        wx.showLoading({
            title: '加载中...',
        })
        const that = this
        navItem.forEach(item => {
            if (item.checked) {
                wx.request({
                    url: that.data.url + item.url,
                    data: {uid: that.data.user.id},
                    success(res) {
                        if (res.data.code === 1) {
                            let temp = res.data.result
                            let list = []
                            console.log(temp);
                            if (temp.length != 0) {
                                temp.forEach(re => {
                                    if (re.createDate) {
                                        list.push(re)
                                    }
                                })
                            }
                            item.list = list
                            item.amount = list.length
                            that.setData({ navItem, list: item.list })
                            wx.hideLoading({
                                success: (res) => { },
                            })
                        }
                    }
                })
            }
        })
    },
    // 取消订单
    cancle(e) {
        const that = this
        wx.showModal({
            title: '温馨提示',
            content: '确认' + e.detail.msg + '订单嘛？',
            success(res) {
                if (res.confirm) {
                    wx.request({
                        url: that.data.url + "/orders/remove?id=" + e.detail.id,
                        success(res) {
                            if (res.data.code == 1) {
                                wx.showToast({
                                    title: '订单成功' + e.detail.msg,
                                    success(res) {
                                        that.initData(that.data.navItem)
                                    }
                                })
                            }
                        }
                    })
                } else if (res.cancel) {
                    wx.showToast({
                        title: '取消',
                        icon: 'error'
                    })
                }
            }
        })
    },
    initNavItem() {

    },
    onShow() {
    },
    // 点击切换菜单
    switchTab(e) {
        const index = e.currentTarget.dataset.index
        let navItem = this.data.navItem
        navItem.forEach(item => { item.checked = false })
        navItem[index].checked = true
        this.setData({ navItem })
        this.initData(navItem)
    }
})